﻿#include "OptionManager.h"
#include <QSettings>
#include <QCoreApplication>
#include <QDir>

#define WorkOptionFile "WorkOption.opt"
// 工作目录内的固定目录，由程序生成
#define FixWorkDir "/FixWorkDir"
// 用户目录
#define UserDir "/UserDir"
OptionManager &OptionManager::Instance()
{
	static OptionManager m_manager;
	return m_manager;
}

const std::string & OptionManager::GetWorkDir()
{
	if (m_workspaceDir.empty())
	{
		QSettings settings(WorkOptionFile, QSettings::IniFormat);
		QString workDir = settings.value("Workspace").toString();
		if (workDir.isEmpty())
		{
			workDir = QDir::currentPath() + "/Workspace";
			settings.setValue("Workspace", workDir);
		}
		QDir pDir(workDir);
		workDir = pDir.absolutePath();
		m_workspaceDir = workDir.toLocal8Bit().data();
	}
	return m_workspaceDir;
}

void OptionManager::SetWorkDir(const std::string &pworkDir)
{
	if (pworkDir.empty())
	{
		return;
	}
	QSettings settings(WorkOptionFile, QSettings::IniFormat);
	settings.setValue("Workspace", QString::fromLocal8Bit(pworkDir.c_str()));
	m_workspaceDir = pworkDir;
}

void OptionManager::SetDefOption(const std::string &pName, const std::string &pVal)
{
	QSettings settings(WorkOptionFile, QSettings::IniFormat);
	settings.setValue(QString::fromLocal8Bit(pName.c_str()), QString::fromLocal8Bit(pVal.c_str()));
}

void OptionManager::SetDefOption(const std::string &pName, const std::list<std::string> &pValList)
{
	QStringList nValList;
	for (std::list<std::string>::const_iterator it = pValList.begin(); it != pValList.end(); ++it)
	{
		nValList << QString::fromLocal8Bit(it->c_str());
	}
	QSettings settings(WorkOptionFile, QSettings::IniFormat);
	settings.setValue(QString::fromLocal8Bit(pName.c_str()), nValList.join(';'));
}

std::string OptionManager::GetDefOption(const std::string &pName, const std::string &pDefVal)
{
	QSettings settings(WorkOptionFile, QSettings::IniFormat);
	QString pVal = settings.value(QString::fromLocal8Bit(pName.c_str()), QString::fromLocal8Bit(pDefVal.c_str())).toString();
	return pVal.toLocal8Bit().data();
}

void OptionManager::GetDefOption(const std::string &pName, std::list<std::string> &outValList)
{
	QSettings settings(WorkOptionFile, QSettings::IniFormat);
	QString pVal = settings.value(QString::fromLocal8Bit(pName.c_str())).toString();
	QStringList nValList = pVal.split(';');
	for (QStringList::iterator it = nValList.begin(); it != nValList.end(); ++it)
	{
		outValList.emplace_back(it->toLocal8Bit().data());
	}
}

std::string OptionManager::GetMarketDataDir()
{
	return GetWorkDir() + FixWorkDir + "/MarketData";
}

std::string OptionManager::GetBaseKItemDir()
{
	return GetWorkDir() + FixWorkDir + "/BaseKData";
}

std::string OptionManager::GetOrderFile()
{
	std::string pDir = GetWorkDir() + FixWorkDir;
	MakeSureDir(pDir);
	return pDir + "/OrderRes.json";
}

std::string OptionManager::GetStrategySettingFile()
{
	std::string pDir = GetStrategyDir();
	MakeSureDir(pDir);
	return pDir + "/StrategySetting.json";
}

std::string OptionManager::GetStrategyDir()
{
	std::string pDir = GetWorkDir() + FixWorkDir + "/StrategyLib";
	return pDir;
}

std::string OptionManager::GetSymbolDir()
{
	std::string pDir = GetWorkDir() + FixWorkDir + "/SymbolGroup";
	MakeSureDir(pDir);
	return pDir + '/';
}

std::string OptionManager::GetGenDir()
{
	return GetWorkDir() + FixWorkDir;
}

std::string OptionManager::GetUsrDir()
{
	return GetWorkDir() + UserDir;
}

void OptionManager::MakeSureDir(const std::string &pDir)
{
	QString qdir = QString::fromLocal8Bit(pDir.c_str());
	QDir dir(qdir);
	if (dir.exists())
	{
		return;
	}
	dir.mkpath(qdir);
}

std::string OptionManager::GetCTPMDAccountSettingFile()
{
	std::string genDir = GetGenDir();
	MakeSureDir(genDir);
	std::string mdFile = genDir + "/CTPMDSetting.ini";
	return mdFile;
}

std::string OptionManager::GetCTPTradeAccountSettingFile()
{
	std::string genDir = GetGenDir();
	MakeSureDir(genDir);
	std::string mdFile = genDir + "/CTPTradeSetting.ini";
	return mdFile;
}

OptionManager::OptionManager()
{
}

OptionManager::~OptionManager()
{
}
